﻿using System;
using System.Linq;
using System.Collections.Generic;
using T.ServiceComponent.I18N.Service;
using T.ServiceComponent.Kernel.Service;

namespace T.FAS.Runtime.Metadata.Entity.DataModel
{
    /// <summary>
    /// InternationalTools
    /// </summary>
    public class InternationalDataTools
    {
        private static readonly object s_lockObj = new object();
        private static Dictionary<string, string> s_languageMapping;
        private static List<string> s_defaultParalanguage = new List<string>
        {
            "Lang1", "Lang2", "Lang3"
        };

        /// <summary>
        /// 获取数据多语言映射关系，例如{"lang1", "zh-CN" }, {"lang2", "en-US" }
        /// </summary>
        public static Dictionary<string, string> LanguageMapping
        {
            get
            {
                if (s_languageMapping == null)
                {
                    lock (s_lockObj)
                    {
                        if (s_languageMapping == null)
                        {
                            s_languageMapping = GetI18NDataMapping();
                        }
                    }
                }
                return s_languageMapping;
            }
        }


        public static List<Language> RTPSupportLanguage => LanguageService.GetSupportLanguages();


        public static List<string> DefaultParalanguage
        {
            get
            {
                return s_defaultParalanguage;
            }
        }

        private static Dictionary<string, string> GetI18NDataMapping()
        {
            var dict = new Dictionary<string, string>();
            try
            {
                //Language中存储的key: Main, Lang1, Lang2
                List<Language> languages = LanguageService.GetSupportLanguages();
                if (languages != null && languages.Count > 0)
                {
                    foreach (var lan in languages)
                    {
                        //排除Main主语言
                        if (!string.IsNullOrEmpty(lan.RepositoryCode) && lan.RepositoryCode != "Main")
                            //辅助语言code命名规则：Main, Lang1, Lang2
                            dict[lan.RepositoryCode] = lan.DisplayCode;
                    }
                }
            }
            catch (Exception e)
            {
                //如果该环境服务不可用，使用mockdata
                dict["Lang1"] = "zh-HK";
                dict["Lang2"] = "en-US";
                //dict["Lang3"] = "";
            }
           
            return dict;
        }
    }
}
